package com.neteast.androidclient.newscenter.activity;

import java.util.ArrayList;
import com.neteast.androidclient.newscenter.R;
import com.neteast.androidclient.newscenter.adapter.InfoListAdapter;
import com.neteast.androidclient.newscenter.client.NewsCenterClient;
import com.neteast.androidclient.newscenter.db.InfoDao;
import com.neteast.androidclient.newscenter.domain.InfoCache;
import com.neteast.androidclient.newscenter.domain.Information;
import com.neteast.androidclient.newscenter.service.WidgetService;
import com.neteast.androidclient.newscenter.util.ConfigUtil;
import com.neteast.androidclient.newscenter.util.ImageUtil;
import com.neteast.androidclient.newscenter.util.LoginUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class NewsListActivity extends Activity implements OnClickListener {
	
	
	private Information currentInformation;
	private Button clearBtn;
	private View topPannel;
	private ListView listView;
	private InfoListAdapter adapter;
	
	private InfoCache cache;
	private InfoDao dao;
	private int userId;
	private MainActivity mainActivity;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newscenter_newslist);
		mainActivity = (MainActivity) getParent();
		init();
	}
	
	private void init() {
		dao = new InfoDao(this);
		dao.open();
		
		userId = LoginUtil.getUserId(this);
		cache=new InfoCache();
		
		
		topPannel = findViewById(R.id.top_pannel);
		clearBtn=(Button) findViewById(R.id.btn_clear);
		Button refreshBtn=(Button) findViewById(R.id.btn_refresh);
		
		listView = (ListView)findViewById(R.id.listview);
		adapter=new InfoListAdapter(this, cache.getData());
		listView.setAdapter(adapter);
		listView.setEmptyView(this.findViewById(R.id.empty));
		
		clearBtn.setOnClickListener(this);
		refreshBtn.setOnClickListener(this);
		listView.setOnItemClickListener(new ListViewClickListener());
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_clear:
			clear();
			break;
		case R.id.btn_refresh:
			refresh();
			break;	
		}
	}
	
	private void clickUserBtn() {
		clearBtn.setVisibility(View.VISIBLE);
		topPannel.setBackgroundResource(R.drawable.bg_top_pannel_user);
		cache.clearCache();
		ArrayList<Information> infos = dao.getInterActiveInfos(userId);
		if (infos!=null) {
			cache.addInfos(infos);
		}
		adapter.notifyDataSetChanged();
	}
	
	private void clickAppBtn() {
		clearBtn.setVisibility(View.VISIBLE);
		topPannel.setBackgroundResource(R.drawable.bg_top_pannel_app);
		cache.clearCache();
		ArrayList<Information> infos = dao.getAppInfos(userId);
		if (infos!=null) {
			cache.addInfos(infos);
		}
		adapter.notifyDataSetChanged();
	}
	
	private void clickSysBtn() {
		clearBtn.setVisibility(View.GONE);
		topPannel.setBackgroundResource(R.drawable.bg_top_pannel_sys);
		cache.clearCache();
		ArrayList<Information> infos = dao.getSysInfos(userId);
		if (infos!=null) {
			cache.addInfos(infos);
		}
		adapter.notifyDataSetChanged();
	}
	/**
	 * 点击清空按钮
	 */
	private void clear() {
		ArrayList<Information> data = cache.getData();
		for (Information info : data) {
			ImageUtil.deleteInfo(info, this);
		}
		cache.clearCache();
		adapter.notifyDataSetChanged();
		if (ConfigUtil.getAutoClose(this, userId)) {
			mainActivity.finish();
		}
		mainActivity.clearNewsNum();
		mainActivity.notifyNewsNum();
	}
	
	/**
	 * 点击刷新按钮
	 */
	public void refresh() {
		userId=LoginUtil.getUserId(this);
//		int type = WidgetUtil.getPressedBtn(this);
		switch (WidgetService.pressBtn) {
		case R.id.btn_sys:
			clickSysBtn();
			break;
		case R.id.btn_app:
			clickAppBtn();
			break;
		case R.id.btn_user:
			clickUserBtn();
			break;
		}
		mainActivity.clearNewsNum();
		mainActivity.notifyNewsNum();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		cache.clearCache();
	}
	
	@Override
	protected void onResume() {
		userId = LoginUtil.getUserId(this);
		super.onResume();
		switch (WidgetService.pressBtn) {
		case R.id.btn_sys:
			clickSysBtn();
			break;
		case R.id.btn_app:
			clickAppBtn();
			break;
		case R.id.btn_user:
			clickUserBtn();
			break;
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		dao.close();
		cache.clearCache();
	}
	
	private class ListViewClickListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			currentInformation = cache.get(position);
			NewsCenterClient.userOptInfos.add(currentInformation.infoId);
		}
	}
}
